﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BBS.Entity;
using BBS.Data.Forum;
using System.Data;
using BBS.Bll.Security;
using BBS.Entity.Exception;

namespace BBS.Bll.Sys
{
    /// <summary>
    /// 用户操作类
    /// </summary>
    public class User
    {
        private AccountTable _table = new AccountTable();
        /// <summary>
        /// 创建用户账号
        /// </summary>
        /// <param name="account">账号</param>
        /// <returns>被创建账号</returns>
        public Account CreateUser(Account account)
        {
            account.Password = MD5.GetMd5Hash(account.Password);
            return _table.CreateAccount(account);
        }
        /// <summary>
        /// 登录账号
        /// </summary>
        /// <param name="email">电子邮箱</param>
        /// <param name="password">密码</param>
        /// <returns>用户账号</returns>
        public Account LogIn(string email,string password)
        {
            string pwd = MD5.GetMd5Hash(password);
            DataTable dt = _table.GetUserByEmailAndPassword(email, pwd);
            if (dt.Rows.Count==0)
            {
                throw new UserLoginException("用户登录失败,用户邮箱或密码不正确");
            }
            var row = dt.Rows[0];
            Account account = new Account();
            account.Birthday = DateTime.Parse(row["birthday"].ToString());
            account.Email = row["email"].ToString();
            account.Gender = bool.Parse(row["gender"].ToString());
            account.Id = int.Parse(row["ID"].ToString());
            account.LastIp = row["last_ip"].ToString();
            account.LastTime = DateTime.Parse(row["last_time"].ToString());
            account.MSN = row["msn"].ToString();
            account.NickName = row["nick_name"].ToString();
            account.Password = row["password"].ToString();
            account.Points = int.Parse(row["points"].ToString());
            account.QQ = row["qq"].ToString();
            account.RegisterTime = DateTime.Parse(row["register_time"].ToString());
            account.Sign = row["sign"].ToString();
            account.Type = (AccountType) Enum.ToObject(typeof (AccountType), int.Parse(row["type"].ToString()));
            return account;
        }
        /// <summary>
        /// 检查邮箱是否可以使用
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <returns>可用true、不可用false</returns>
        public bool CheckEmail(string email)
        {
            return _table.CheckEmailAndNickName(email, null) == 0;
        }
        /// <summary>
        /// 检查昵称是否可用
        /// </summary>
        /// <param name="nickName">昵称</param>
        /// <returns>可用true、不可用false</returns>
        public bool CheckNickName(string nickName)
        {
            return _table.CheckEmailAndNickName(null, nickName) == 0;
        }
        /// <summary>
        /// 检查密码是否满足要求
        /// </summary>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public bool CheckPassword(string password)
        {
            //必须大于6位密码
            return password.Length > 5;
        }
    }
}
